package com.qiguliuxing.dts.wx.service;

import cn.binarywang.wx.miniapp.api.WxMaLinkService;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.urllink.GenerateUrlLinkRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.bean.menu.WxMenu;
import me.chanjar.weixin.common.bean.menu.WxMenuButton;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.menu.WxMpGetSelfMenuInfoResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class WxMpToolsService {
	private static final Logger logger = LoggerFactory.getLogger(WxMpToolsService.class);

	@Autowired
	private WxMpService wxMpService;
	@Autowired
	private WxMaLinkService wxMaLinkService;

	public void syncMenu() {
		logger.info("开始同步公众号菜单");
        try {
			/*GenerateUrlLinkRequest generateUrlLinkRequest = new GenerateUrlLinkRequest();
			generateUrlLinkRequest.setPath("pages/index/index");
            wxMaLinkService.generateUrlLink(generateUrlLinkRequest);*/
			//菜单
			WxMenu wxMenu = new WxMenu();
			// 创建按钮B
			WxMenuButton buttonA = new WxMenuButton();
			buttonA .setType(WxConsts.MenuButtonType.VIEW);
			buttonA.setName("授权绑定");
			buttonA.setUrl("https://lxyzxt.cn/dts/index.html");

			// 将按钮A和按钮B添加到菜单
			wxMenu.getButtons().add(buttonA);
			wxMpService.getMenuService().menuCreate(wxMenu);

			// 获取菜单信息
			WxMpGetSelfMenuInfoResult selfMenuInfo = wxMpService.getMenuService().getSelfMenuInfo();
			logger.info("selfMenuInfo : {}", selfMenuInfo);
		} catch (WxErrorException e) {
			logger.error("同步公众号菜单失败：{}",e.getMessage());
		}
	}

}
